﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDataService;
using System.Data;
using QuanLyBanHang.Report;

namespace QuanLyBanHang.DAL
{
  public class DalNhapHang
    {
      DalBanHang banHang=new DalBanHang();
      public bool NhapHangHoa(string MaHang,int SoLuong,string GiaTien)
      {
          bool Result = true;
          string GetMHDN = "";

          int CountHDN = int.Parse(dataservice.selectScalar("select COUNT(MaHDN) from HoaDonNhap").ToString());
          CountHDN += 1;
          string CountMHDN = string.Format("{0:HDN0000000}", CountHDN);
          GetMHDN =CountMHDN;
          int slCu = banHang.exMaVach(MaHang);
          slCu += SoLuong;

          string query = "insert into HoaDonNhap values('" + GetMHDN + "','" + SessionPrivate.NhanVien.MaNV +
                                    "',GETDATE()) insert into CT_HoaDonNhap values('" + GetMHDN + "','" + MaHang + "'," +
                                    SoLuong + ",'" + GiaTien + "')";
          if (!dataservice.runQuery(query))
          {
              Result = false;
          }
          else
          {
              if(!dataservice.runQuery("Update HangHoa set SoLuong ="+slCu+" where MaVach='"+MaHang+"'"))
          {
              Result = false;
          }
          }
          frmInHoaDon frmInHoaDon = new frmInHoaDon(GetMHDN);
          frmInHoaDon.ShowDialog();
          return Result;





      }
      public bool exMaVach(string MaVach)
      {
          bool Result = false;
          DataTable dt = dataservice.selectDatatable("select MaHang from HangHoa where MaVach='" + MaVach + "'");
          if (dt.Rows.Count>0)
          {
              Result = true;
          }
          return Result;
      }
    }
}
